package com.ll.generic;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author 刘龙
 * @version 1.0
 */
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("ll", 20000, new MyDate(2020, 02, 06)));
        employees.add(new Employee("ll", 15000, new MyDate(2020, 01, 03)));
        employees.add(new Employee("jack", 5000, new MyDate(2023, 03, 03)));

        System.out.println(employees);
        System.out.println("进行排序");
        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee emp1, Employee emp2) {
                // 先按照name排序,如果name相同, 就按生日日期的先后排序
                // 先对传入的参数进行验证
                if (!(emp1 instanceof Employee && emp2 instanceof Employee)) {
                    System.out.println("类型不匹配");
                    return 0;
                }
                // 比较名字
                int i = emp1.getName().compareTo(emp2.getName());
                if (i != 0) {
                    return i;
                }

                // 把对年月日的比较封装到MyDate中
                return emp1.getMyDate().compareTo(emp2.getMyDate());
            }
        });

        System.out.println(employees);
    }
}
